﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Nofs.Net.AnnotationDriver;

namespace nofs.fuse.test
{
    class ClassLoaderTest
    {
        private Assembly Loader;
        public ClassLoaderTest(string fileName)
        {
            Loader = Assembly.LoadFile(fileName);
        }

        public IDictionary<string, List<string>> LoadAllClass()
        {
            Dictionary<string, List<string>> map = new Dictionary<string, List<string>>();

            foreach (Type item in Loader.GetTypes() )
            {
                if (!item.IsInterface)
                {
                    foreach (object o in item.GetCustomAttributes(true))
                    {
                        if (o is Attribute)
                        {
                            Attribute attr = o as Attribute;
                            Type t = attr.GetType();

                            if (t == typeof(RootFolderObjectAttribute))
                            {
                                AddTypeToMap("RootFolderOjbect", item.Name, map);
                            }
                            else if (t == typeof(FolderObjectAttribute))
                            {
                                AddTypeToMap("FolderObject", item.Name, map);
                            }
                            else if (t == typeof(DomainObjectAttribute))
                            {
                                AddTypeToMap("DomainObject", item.Name, map);
                            }
                            else if (t == typeof(ExecutableAttribute))
                            {
                                AddTypeToMap("Executable", item.Name, map);
                            }
                        }
                    }

                    foreach (MethodInfo info in item.GetMethods())
                    {
                        foreach (object o in info.GetCustomAttributes(true))
                        {
                            if (o is Attribute)
                            {
                                Attribute attr = o as Attribute;
                                Type t = attr.GetType();

                                if (t == typeof(RootFolderObjectAttribute))
                                {
                                    AddTypeToMap("RootFolderOjbect", info.Name, map);
                                }
                                else if (t == typeof(FolderObjectAttribute))
                                {
                                    AddTypeToMap("FolderObject", info.Name, map);
                                }
                                else if (t == typeof(DomainObjectAttribute))
                                {
                                    AddTypeToMap("DomainObject", info.Name, map);
                                }
                                else if (t == typeof(ExecutableAttribute))
                                {
                                    AddTypeToMap("Executable", info.Name, map);
                                }
                            }
                        }
                    }

                }
            }


            return map;
        }

        private void AddTypeToMap(string key, string type, Dictionary<string, List<string>> map)
        {
            if (!map.ContainsKey(key))
            {
                map.Add(key, new List<string>());
            }
            map[key].Add(type);
        }
    }
}
